# Mike, Ellie, Rohan, Fatma四个人参加自行车比赛，分别获得1-4名
# 他们每个人骑不同颜色的自行车(红, 紫, 蓝，绿）
# Ellie骑紫色车，Rohan没有骑绿色的车，Mike和 Ellie不是第一也不是最后
# 骑蓝色车的人领先 Fatma，骑绿色车的人领先骑紫色车的人
import itertools
import time

players = ['Mike', 'Ellie', 'Rohan', 'Fatma']
bikes = ['red', 'purple', 'blue', 'green']
people_data = []


def diff_4(x1, x2, x3, x4):
    if x1 != x2 and x1 != x3 and x1 != x4 and x2 != x3 and x2 != x4 and x3 != x4:
        return True
    return False


# 暴力法,把所有情况全部遍历一遍
# 但是这样太慢了
def violent_method():
    count = 0
    start = time.time()
    for Mike in range(1, 5):
        for Ellie in range(1, 5):
            for Rohan in range(1, 5):
                for Fatma in range(1, 5):
                    for red in range(1, 5):
                        for purple in range(1, 5):
                            for blue in range(1, 5):
                                for green in range(1, 5):
                                    count = count + 1
                                    if Ellie == purple and Rohan != green \
                                            and Mike != 1 and Mike != 4 \
                                            and Ellie != 1 and Ellie != 4 \
                                            and blue < Fatma and green < purple \
                                            and diff_4(Ellie, Mike, Rohan, Fatma) \
                                            and diff_4(red, purple, blue, green):
                                        print('Mike %d, Ellie %d, Rohan %d, Fatma %d' % (Mike, Ellie, Rohan, Fatma))
                                        print('red %d, purple %d, blue %d, green %d\n' % (red, purple, blue, green))
                                        break
    end = time.time()
    print('violent_method', end - start, count)
    return end - start


# 应该有更好的算法
def violent_method2():
    count = 0
    start = time.time()
    for Mike in range(2, 4):
        for Rohan in range(1, 5):
            for Fatma in range(1, 5):
                for red in range(1, 5):
                    for blue in range(1, 5):
                        for green in range(1, 5):  # 可以去掉 Rohan
                            for Ellie in range(green, 4):
                                count = count + 1
                                if Rohan != green \
                                        and blue < Fatma and green < Ellie \
                                        and diff_4(Ellie, Mike, Rohan, Fatma) \
                                        and diff_4(red, Ellie, blue, green):
                                    print('Mike %d, Ellie %d, Rohan %d, Fatma %d' % (Mike, Ellie, Rohan, Fatma))
                                    print('red %d, purple %d, blue %d, green %d\n' % (red, Ellie, blue, green))
                                    break
    end = time.time()
    print('violent_method2', end - start, count)
    return end - start


def full_permutation():
    count = 0
    start = time.time()
    array = [1, 2, 3, 4]
    peoples = list(itertools.permutations(array))
    colors = peoples.copy()
    # players = ['Mike', 'Ellie', 'Rohan', 'Fatma']
    # bikes = ['red', 'purple', 'blue', 'green']
    # Ellie骑紫色车，Rohan没有骑绿色的车，Mike和 Ellie不是第一也不是最后
    # 骑蓝色车的人领先 Fatma，骑绿色车的人领先骑紫色车的人
    for people in peoples:
        for color in colors:
            count = count + 1
            if people[1] == color[1] and people[2] != color[3] \
                    and people[0] != 1 and people[0] != 4 \
                    and people[1] != 1 and people[1] != 4 \
                    and color[2] < people[3] \
                    and color[3] < color[1]:
                print(people, color)
                break
    end = time.time()
    print('full_permutation', end - start, count)
    pass


t1 = violent_method()
t2 = violent_method2()
full_permutation()
